/**
 * 用户注册
 * @type {{regAction: string}}
 */
var urlParams = {
    regAction: webRoot + "/login/register",
    sendAction: webRoot + "/login/sendCode"
};

$(function () {
    initEvent();
    $(".form-data").delegate(".send", "click", function () {
        var phone = $.trim($('#tel').val());
        if (MatchesClass.checkPhone(phone)) {
            $.ajax({
                url: urlParams.sendAction,
                type: 'post',
                dataType: 'json',
                async: true,
                data: {phoneNumber: phone, type: "login"},
                success: function (data) {
                    if (data.code == '0') {

                    } else {

                    }
                },
                error: function () {

                }
            });
            var oTime = $(".form-data .time"),
                oSend = $(".form-data .send"),
                num = parseInt(oTime.text()),
                oEm = $(".form-data .time em");
            $(this).hide();
            oTime.removeClass("hide");
            var timer = setInterval(function () {
                var num2 = num -= 1;
                oEm.text(num2);
                if (num2 == 0) {
                    clearInterval(timer);
                    oSend.text("重新发送验证码");
                    oSend.show();
                    oEm.text("60");
                    oTime.addClass("hide");
                }
            }, 1000);
        }
    });
});

/**
 * 添加监听
 */
function initEvent() {
    $(".lang-btn").click(function () {
        var inp = $("#tel").val();
        var checkPhone = MatchesClass.checkPhone(inp);
        if (checkPhone != "success") {
            return alert(checkPhone);
        } else {
            var passport = $("#passport").val();
            var passport2 = $("#passport2").val();
            var checkPass = MatchesClass.checkPass(passport);
            if (passport.length != passport2.length) {
                return alert("两次密码输入不一致!");
            } else if (checkPass != "success") {
                return alert(checkPass);
            }
            var pass = $.md5(passport);
            $.ajax({
                type: 'post',
                url: urlParams.regAction,
                cache: false,
                data: {phoneNumber: inp, userName: inp, password: pass,verCode:$("#veri-code").val()},
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        alert(data.msg);
                        if (data.success) {
                            window.location.href = webRoot + "login/user";
                        }
                    }
                },
                error: function () {
                }
            });
        }
    });
}